# Boyoon Lee "The Impact of Educational Content on Anti-Immigrant Attitudes"
# 2. Online Appendix B.2 (Figure B.4)
# Last updated: 2022-11-16


# Initial settings --------------------------------------------------------
# Set directory
setwd("Your path here")

# Packages
library(ggplot2) 


# Load data  --------------------------------------------------------------

### Original data
data <- read.csv("./cleaned_TSCS_did.csv", header=TRUE, sep=",")


# Recode / Re-class  ------------------------------------------------------

### Make date variable using Birth year and Birth month
# Since there is no date of birth available, I set the date to the first day of the month
data$date <- as.Date(with(data, paste(birth_yr, birth_month, "01", sep="-")), "%Y-%m-%d")

### Only leave school cohorts before 2002 
# In 2001, Knowing Taiwan series discontinued and replaced with the New Grade 1-9 curriculum (9 year curriculum)
# In addition, from 1968, junior vocational school shut down and expanded senior vocational school
data<-subset(data, data$school_yr<2002&data$school_yr>1967)


# Subsets for the Comparison Group ----------------------------------

# Main comparison group 
yr13_22<-subset(data,school_yr>1987 & school_yr<2002)
yr13_22<-subset(yr13_22,edu_level>3) # those who graduated from at least junior high schools




#######################################################
##       [APPENDIX B.2] Sample Distribution          ##
#######################################################


# Figure B.4.  -------------------------------------------------------

p<-ggplot(yr13_22, aes(x=date, y=loc_birth)) + geom_bin2d(bins=21) + scale_fill_gradient(low = "#cccccc", high = "#09005F") +
  geom_vline(xintercept = as.numeric(as.Date("1984-09-01")), color = "red", size=0.5, linetype="dashed")
p + theme_bw() + labs(x = "Birth Year", y = "County / City") + scale_y_continuous(breaks=c(22.5,21.5,20.5,19.5,18.5,17.5,16.5,15.5,14.5,13.5,
                                                                                           12.5,11.5,10.5,9.5,8.5,7.5,6.5,5.5,4.5,3.5,2.5,1.5),
                                                                                  labels=c("Lienchiang County","Kinmen County","Penghu County",
                                                                                           "Ilan county","Hualien county","Taitung County",
                                                                                           "Pingtung County","Kaohsiung city","Tainan city",
                                                                                           "Chiayi County","Chiayi city","Yunlin County",
                                                                                           "Nantou County","Changhua County","Taichung city",
                                                                                           "Miaoli County","Hsinchu County","Hsinchu city",
                                                                                           "Taoyuan county","New Taipei city","Taipei city",
                                                                                           "Keelung city")) +
  theme(text = element_text(size = 15),panel.grid.major.y = element_blank())

